package com.vast.admin.controller;

import com.vast.admin.api.CietacApi;
import com.vast.admin.entity.ContractDetail;
import com.vast.admin.service.ContractDetailService;
import com.vast.admin.service.ContractService;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import com.vast.system.service.ISysUserService;
import com.vast.system.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 开发使用
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private ContractDetailService contractDetailService;

    @Autowired
    private ISysUserService managerService;

    @Autowired
    private CietacApi testApi;

    /**
     * 针对由于并发问题导致未锁定的合同
     * @param id
     * @return
     */
    @GetMapping("/test1/{id}")
    public Result<Void> test1(@PathVariable Long id) {
        if (id == null) {
            return Result.ERROR;
        }
        List<ContractDetail> contractDetails = contractDetailService.getByContractId(id);
        if (contractDetails == null || contractDetails.isEmpty()) {
            return Result.ERROR;
        }
        for (ContractDetail contractDetail : contractDetails) {
            if (contractDetail.getStatus() != 3) {
                return Result.ERROR;
            }
        }
        contractService.completeContract(contractDetails);
        return Result.SUCCESS;
    }

    /**
     * 下载合同存证页
     * @param response
     * @param contractId
     * @return
     */
    @GetMapping("/test4")
    public Result<Void> test4(HttpServletResponse response, Long contractId) {
        contractService.downloadAttachment(response, contractId);
        return Result.SUCCESS;
    }

    /**
     * 对外token生成
     * @param id
     * @return
     * @throws ParseException
     */
    @GetMapping("/test5")
    public Result<String> test5(String id) throws ParseException {
        SysUser manager = managerService.getManagerInfoById(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return new Result<>(MessageCode.SUCCESS, JWTUtil.sign(manager.getUserId() + "", manager.getPassword(), simpleDateFormat.parse("2099-12-31")));
    }

    @GetMapping("/test8")
    public Result<Void> test8() {
        String a = testApi.getProjectTypeRemark(51L).getData().getProject_type_remark();
        log.info(a);


//        Respondent respondent = new Respondent();
//        respondent.setType("1");
//        respondent.setMobile("19986929224");
//        respondent.setBase_region("420001");
//        respondent.setAddress("123123123");
//
//        Evidence evidence = new Evidence();
//        evidence.setEvidenceName("测试测试测试测试");
//        evidence.setEvidenceRemark("测试测试测试测试");
//        String path = "C:\\Users\\wuyufei\\Desktop\\1617259435(1).jpg";
//        MultipartFile multipartFile = new CommonsMultipartFile(createFileItem(path));
//        evidence.setEvidenceAttachment(multipartFile);

//        CietacContract cietacContract = new CietacContract();
//        cietacContract.setContractAttachment(multipartFile);
//        cietacContract.setContractName("测试合同名称");
//        cietacContract.setContractNo("123123123");
//        cietacContract.setContractSignTime("2021-05-13");
//
//        Result<CietacLawcaseId> cietacLawcaseIdResult =
//                testApi.submitCase("18696192086", new CietacLawcaseInfo(51L, new BigDecimal(1000), 2), Arrays.asList(new Respondent[]{respondent}), Arrays.asList(new Evidence[]{evidence}), cietacContract);
        return Result.SUCCESS;
    }

    private FileItem createFileItem(String filePath) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "textField";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName");
        File newfile = new File(filePath);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(newfile);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }


}
